<?php
/*
 * ImageComponent for CakePHP
 *
 * @author      gwoo <gwoo@rd11.com>
 * @version     0.10.8.2176
 * @license		OPPL
 *
 */

class rdImageComponent {

	var $ImageStream;
	var $ImagePath;
	var $ImageURL;
	
	var $_tmpPath;
	var $_width;
	var $_height;
	var $_type;
	var $_mimetype;

	var $reset;
	var $errors = array();
	var $dir;
	var $extension;
    var $filename;
	var $font = 'arial';
	var $maxWidth = '4800';	
	var $maxHeight = '4800';

	var $interlace = false;
	var $jpegquality = '100';
	var $orientation;
	var $resizeWidth = array('100','460');
	var $resizeHeight = array('100','360');
    var $webroot;
    
	
	function getErrors() 
	{
		if(DEBUG == 1) 
		{
			return $this->errors;
		}
	}

	function loadtmpfile($filename) 
	{
		if(!extension_loaded('gd'))
		{
			$this->errors[] = 'error #001: we need gd library';
			return false; 
		}
		/* load an image from tmp into memory */
		$this->_tmpPath = $filename;
		
		if (file_exists($this->_tmpPath)) 
		{		
			$this->_initializeImageProperties($this->_tmpPath);
			$this->_loadImage($this->_tmpPath);
		}
		else 
		{
			$this->errors[] = 'error #2001: file could not be loaded';
			return false; 			
		}
	
	}
	
	function loadfile($filename) 
	{
		if(!extension_loaded('gd'))
		{
			$this->errors[] = 'error #001: we need gd library';
			return false; 
		}
		/* load an image from file into memory */
		$this->_setLocations($filename);
		
		if (file_exists($this->_tmpPath)) 
		{		
			$this->_initializeImageProperties($this->_tmpPath);
			$this->_loadImage($this->_tmpPath);
		}
		elseif(file_exists($this->ImagePath))
		{
			$this->_initializeImageProperties($this->ImagePath);
			$this->_loadImage($this->ImagePath);
		}
		else 
		{
			$this->errors[] = 'error #2001: file could not be loaded';
			return false; 			
		}
	
	}
	
	function savefile($filename) 
	{
		if(!extension_loaded('gd'))
		{
			$this->errors[] = 'error #001: we need gd library';
			return false; 
		}
		/* store memory image to file */
		if ((isset($filename)) && ($filename != '')) 
		{
			$this->_setLocations($filename);
		}	
		
		$this->_saveImage();
		//set this if you want to reset to tmp path when uploading multiple files
		if($this->reset == 1)
		{
			$this->loadtmpfile($this->_tmpPath);
		}

	}	
	
	function _setLocations($filename) 
	{
		$this->filename = $filename;
		$this->ImagePath = WWW_ROOT.$this->dir.DS.$filename;
		$this->ImageUrl = $this->webroot.$this->dir.'/'.$filename;
	}
	
	function _initializeImageProperties($file) 
	{
		/* get imagesize from file and set imagesize array */
		list($this->_width, $this->_height, $iType, $this->htmlattributes) = getimagesize($file);
		
		if (($this->_width < 1) || ($this->_height < 1)) 
		{
			$this->errors[] = 'error #2002: the image is did not load properly'; 			
		}	
		if($this->_width > $this->maxWidth)
		{
			$this->errors[] = 'error #2003: the width of photo '.$this->filename.' is too large. try resizing the width to something less than '.$this->maxWidth.'px';
			return false;
		}
		elseif($this->_height > $this->maxHeight)
		{
			$this->errors[] = 'error #2004: the height of photo '.$this->filename.' is too large. try resizing the height to something less than '.$this->maxHeight.'px';
			return false;
		}
		
		$this->_setImageOrientation();
		$this->_setImageType($iType);
	}
	
	function _setImageOrientation() 
	{
		/* get image-orientation based on imagesize
		options: [ portrait | landscape | square ] */
		
		if ($this->_width < $this->_height) 
		{
			$this->_ratio = $this->_height / $this->_width;
			$this->orientation = 'portrait';
		}
		
		if ($this->_width > $this->_height) 
		{
			$this->_ratio = $this->_width / $this->_height;
			$this->orientation = 'landscape';
		}
		
		if ($this->_width == $this->_height) 
		{
			$this->_ratio = '1';
			$this->orientation = 'square';
		}							
	}	

	function _loadImage($file) 
	{
		/* load a image from file */
		switch($this->_type) 
		{
			case 1: 
				$this->ImageStream = @imagecreatefromgif($file);
			break;
			
			case 2:
				$this->ImageStream = @imagecreatefromjpeg($file);			
			break;
			
			case 3: 
				$this->ImageStream = @imagecreatefrompng($file);			
			break;
			
			default: 
			$this->errors[] = 'error #401: image not loaded'; 
			return false;
		}	
		
		if (!$this->ImageStream) 
		{
			$this->errors[] = 'error #402: image not loaded';
			return false;
		}		
	}
	
	function _saveImage() 
	{
		/* store a memoryimage to file */
		
		if (!$this->ImageStream) 
		{
			$this->errors[] = 'error #403 image not loaded'; 
			return false;
		}
		
		switch($this->_type) 
		{
			case 1: 
				/* store a interlaced gif image */
				if ($this->interlace === true) 
				{
					imageinterlace($this->ImageStream, 1);
				}
				
				imagegif($this->ImageStream,$this->ImagePath);
			break;
			
			case 2:
				/* store a progressive jpeg image (with default quality value)*/
				if ($this->interlace === true) 
				{
					imageinterlace($this->ImageStream, 1);
				}
				imagejpeg($this->ImageStream,$this->ImagePath,$this->jpegquality);							
			break;
			
			case 3: 
				/* store a png image */
				imagepng($this->ImageStream,$this->ImagePath);			
			break;
			
			default: 
				$this->errors[] = 'error #104: wrong image type: gif, jpg, png only';
//				return false;			
				
				if (!file_exists($this->ImagePath)) 
				{
					$this->errors[] = 'error #105: image not saved';
					return false;
				}	
							
		}			
	}		

	
	function _setImageType($type) 
	{
		/* set the image type and mimetype */
		switch($type) 
		{
			case 1: 
				$this->_type = $type;
				$this->_mimetype = 'image/gif';
				$this->extension = ".gif";							
			break;
			
			case 2:
				$this->_type = $type;
				$this->_mimetype = 'image/jpeg';
				$this->extension = ".jpg";							
			break;
			
			case 3: 
				$this->_type = $type;
				$this->_mimetype = 'image/png';
				$this->extension = ".png";							
			break;
			
			default: 
				$this->errors[] = 'error #109: wrong image type: gif, jpg, png only'; 			
		}			
	}
	
	function _setExtension($type) 
	{
		/* set the image type and mimetype */
		
		$sOldFilenameExtension = substr($type,strlen($type) - 4, 4);
		
		if (($sOldFilenameExtension != '.gif') &&
		($sOldFilenameExtension != '.jpg') &&
		($sOldFilenameExtension != '.png')) 
		{
			$this->errors[] = 'error #107: wrong image type: gif, jpg, png only';
			return false;		
		}
		else
		{
			$this->extension = $sOldFilenameExtension;
		}
		
		switch($this->_type) 
		{
			case 1: 
				$this->extension = substr($this->filename,0,strlen($this->filename) - 4) . '.gif';
			break;
			
			case 2:
				$this->extension = substr($this->filename,0,strlen($this->filename) - 4) . '.jpg';			
			break;
			
			case 3: 
				$this->extension = substr($this->filename,0,strlen($this->filename) - 4) . '.png';			
			break;
			
			default: 
				$this->errors[] = 'error #108: wrong image type: gif, jpg, png only'; 			
		}			
	}
	
	function preview() 
	{
		/* print memory image to screen */
		$t = "/-/data:{$this->_mimetype};";
		$t .= $this->returnImage();
		return $t;
	}	
	function returnImage() 
	{
		/* show a memoryimage to screen */
		if (!$this->ImageStream) 
		{
			$this->errors[] = 'error #404: image not loaded'; 
			return false;
		}
		
		switch($this->_type) 
		{
			case 1:
				ob_start();
				imagegif($this->ImageStream);
				return ob_get_clean();
			break;
			
			case 2:
				ob_start();
				imagejpeg($this->ImageStream);	
				return ob_get_clean();		
			break;
			
			case 3: 
				ob_start();
				imagepng($this->ImageStream);
				return ob_get_clean();			
			break;
			
			default: 
				$this->errors[] = 'error #106: wrong image type: gif, jpg, png only';
				return false;		
		}			
	}
	function resize($iNewWidth, $iNewHeight) 
	{
		/* resize the memoryimage do not keep ratio */
		if (!$this->ImageStream) 
		{
			$this->errors[] = 'error #405: image not loaded'; 
			return false;
		}	 
		
		if(function_exists("imagecopyresampled"))
		{
			$ResizedImageStream = imagecreatetruecolor($iNewWidth, $iNewHeight);
			imagecopyresampled($ResizedImageStream, $this->ImageStream, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $this->_width, $this->_height);
		}
		else
		{
			$ResizedImageStream = imagecreatetruecolor($iNewWidth, $iNewHeight);
			imagecopyresized($ResizedImageStream, $this->ImageStream, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $this->_width, $this->_height);
		}		
		
		$this->ImageStream = $ResizedImageStream;
		$this->_width = $iNewWidth;
		$this->_height = $iNewHeight;
		$this->_setImageOrientation();		
	}	
	
	function resizetowidth($iNewWidth) 
	{
		/* resize image to given width (keep ratio) */
		if (!$this->ImageStream) 
		{
			$this->errors[] = 'error #406: image not loaded'; 
			return false;
		}	 
		
		/* calculate new height (calculate with ratio) */
		$iNewHeight = ($iNewWidth / $this->_width) * $this->_height;
		
		if(function_exists("imagecopyresampled"))
		{
			$white = imagecolorresolve($this->ImageStream, 255, 255, 255); // resolve given palette entry
			imagecolortransparent($this->ImageStream, $white);
			
			$ResizedImageStream = imagecreatetruecolor($iNewWidth, $iNewHeight);
			imagecopyresampled($ResizedImageStream, $this->ImageStream, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $this->_width, $this->_height);
		
		}
		else
		{
			$ResizedImageStream = imagecreatetruecolor($iNewWidth, $iNewHeight);
			imagecopyresized($ResizedImageStream, $this->ImageStream, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $this->_width, $this->_height);
		}		
		
		$this->ImageStream = $ResizedImageStream;
		$this->_width = $iNewWidth;
		$this->_height = $iNewHeight;
		$this->_setImageOrientation();		
	}	
	
	function resizetoheight($iNewHeight) 
	{
		/* resize image to given height (keep ratio) */
		if (!$this->ImageStream) {
			$this->errors[] = 'error #407: image not loaded'; 
			return false;
		}	 
		
		/* calculate new width (calculate with ratio) */
		$iNewWidth = ($iNewHeight / $this->_height) * $this->_width;
		
		if(function_exists("imagecopyresampled"))
		{
			$ResizedImageStream = imagecreatetruecolor($iNewWidth, $iNewHeight);
			imagecopyresampled($ResizedImageStream, $this->ImageStream, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $this->_width, $this->_height);
		}
		else
		{
			$ResizedImageStream = imagecreatetruecolor($iNewWidth, $iNewHeight);
			imagecopyresized($ResizedImageStream, $this->ImageStream, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $this->_width, $this->_height);
		}		
		
		$this->ImageStream = $ResizedImageStream;
		$this->_width = $iNewWidth;
		$this->_height = $iNewHeight;
		$this->_setImageOrientation();		
	}	
	
	function resizetopercentage($iPercentage) 
	{
		/* resize image to given percentage (keep ratio) */
		if (!$this->ImageStream) 
		{
			$this->errors[] = 'error #408: image not loaded'; 
			return false;
		}	 
		
		/* calculate percentage multiplier*/
		$iPercentageMultiplier = $iPercentage / 100;
		$iNewWidth = $this->_width * $iPercentageMultiplier;
		$iNewHeight = $this->_height * $iPercentageMultiplier;		
		
		$this->resize($iNewWidth,$iNewHeight);		
	}	
	
	function crop($iNewWidth, $iNewHeight, $iResize = 0) 
	{
		/* crop image (first resize with keep ratio) */
		if (!$this->ImageStream) 
		{
			$this->errors[] = 'error #409: image not loaded'; 
			return false;
		}	
		
		/* resize imageobject in memory if resize percentage is set */
		if ($iResize != 0) 
		{
			$this->resizetopercentage($iResize);
		}		 
		
		/* constrain width and height values */
		if (($iNewWidth > $this->_width) || ($iNewWidth < 0)) 
		{
			$this->errors[] = 'error #115: width out of range'; 
			return false;
		} 
		
		if (($iNewHeight > $this->_height) || ($iNewHeight < 0)) 
		{
			$this->errors[] = 'error #116: height out of range'; 
			return false;
		}	
		
		/* create blank image with new sizes */
		$CroppedImageStream = ImageCreateTrueColor($iNewWidth,$iNewHeight);
		
		/* calculate size-ratio */
		$iWidthRatio = $this->_width / $iNewWidth;
		$iHeightRatio = $this->_height / $iNewHeight;
		$iHalfNewHeight = $iNewHeight / 2;
		$iHalfNewWidth = $iNewWidth / 2;
		
		/* if the image orientation is landscape */
		if($this->orientation == 'landscape') 
		{
			/* calculate resize width parameters */
			$iResizeWidth = $this->_width / $iHeightRatio;
			$iHalfWidth = $iResizeWidth / 2;
			$iDiffWidth = $iHalfWidth - $iHalfNewWidth;
			
			if(function_exists("imagecopyresampled"))
			{
				imagecopyresampled($CroppedImageStream,$this->ImageStream,-$iDiffWidth,0,0,0,$iResizeWidth,$iNewHeight,$this->_width,$this->_height);
			}
			else 
			{
				imagecopyresized($CroppedImageStream,$this->ImageStream,-$iDiffWidth,0,0,0,$iResizeWidth,$iNewHeight,$this->_width,$this->_height);
			}		
		}
		/* if the image orientation is portrait or square */
		elseif(($this->orientation == 'portrait') || ($this->orientation == 'square')) 
		{
			/* calculate resize height parameters */		
			$iResizeHeight = $this->_height / $iWidthRatio;
			$iHalfHeight = $iResizeHeight / 2;
			$iDiffHeight = $iHalfHeight - $iHalfNewHeight;
			
			if(function_exists("imagecopyresampled"))
			{
				imagecopyresampled($CroppedImageStream,$this->ImageStream,0,-$iDiffHeight,0,0,$iNewWidth,$iResizeHeight,$this->_width,$this->_height);
			}
			else 
			{
				imagecopyresized($CroppedImageStream,$this->ImageStream,0,-$iDiffHeight,0,0,$iNewWidth,$iResizeHeight,$this->_width,$this->_height);
			}	
		}
		else 
		{ 
			if(function_exists("imagecopyresampled"))
			{
				imagecopyresampled($CroppedImageStream,$this->ImageStream,0,0,0,0,$iNewWidth,$iNewHeight,$this->_width,$this->_height);
			}
			else 
			{
				imagecopyresized($CroppedImageStream,$this->ImageStream,0,0,0,0,$iNewWidth,$iNewHeight,$this->_width,$this->_height);
			}	
		}		
		
		$this->ImageStream = $CroppedImageStream;
		$this->_width = $iNewWidth;
		$this->_height = $iNewHeight;
		$this->_setImageOrientation();											
	}				
	
	function writetext($sText, $font = 'arial', $iFontSize = 10, $sTextColor = '0,0,0', $iXPos = 5, $iYPos = 15, $iTextAngle = 0) 
	{
		/* write text on image */
		if (!$this->ImageStream) 
		{
			$this->errors[] = 'error #410: image not loaded'; 
			return false; 
		} 
		
		if (($iXPos > $this->_width) || ($iXPos < 0)) 
		{
			$this->errors[] = 'error #118: x-pos out of range'; 
			return false;
		} 
		
		if (($iYPos > $this->_height) || ($iYPos < 0)) 
		{
			$this->errors[] = 'error #119: y-pos out of range'; 
			return false;
		}				
		
		$sFont = $this->font;
		$aTextColor = explode(',',$sTextColor,3);
		$ImageColor = imagecolorallocate($this->ImageStream,$aTextColor[0],$aTextColor[1],$aTextColor[2]);
		$iLineWidth = imagettfbbox($iFontSize, $iTextAngle, $sFont, $sText);
		imagettftext($this->ImageStream, $iFontSize, $iTextAngle, $iXPos, $iYPos, $ImageColor, $sFont, $sText);			
	}		
	
	function convert($sTargetType) 
	{
		/* convert image to given type [ jpg | gif | png ] */
		if (!$this->ImageStream) 
		{
			$this->errors[] = 'error #411: image not loaded'; 
			return false;
		} 
		
		switch($sTargetType) 
		{
			case 'gif': 
				$this->_setImageType(1);
			break;
			
			case 'jpg': 
				$this->_setImageType(2);
			break;
			
			case 'png': 
				$this->_setImageType(3);
			break;
			
			default: 
				$this->errors[] = 'error #120: wrong image type: gif, jpg, png only';
			return false;
		}	  
	}	
}
?>